<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * 导航条
 *
 * @category	Admin
 */
class NavigationAction extends AdminAction
{

	protected $Navigation;


	public function _initialize()
	{
		parent::_initialize();
		$this->Navigation = D('Navigation');
	}


	//导航链接列表
	public function navigationList()
	{
		import('ORG.Util.Page');

		$order = array('position' => 'desc');
		$total = $this->Navigation->count();
		$Page = new Page($total);
		$pagination = $Page->show();
		$navigationList = $this->Navigation->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign('pagination', $pagination);
		$this->assign('navigationList', $navigationList);
		$this->display();
	}


	//新增导航
	public function addNavigation()
	{
		$this->editNavigation();
	}


	//编辑导航
	public function editNavigation()
	{
		$navID = isset($_REQUEST['nav_id']) ? intval($_REQUEST['nav_id']) : 0;
		$navInfo = $this->Navigation->find($navID);

		if ($this->isPost())
		 {
		    $_validate = array(
		        array('title', 'require', '{%nav.nav_title_required}'),
		        array('url', 'url', '{%nav.nav_url_invalid}'),
		    );
		    $this->Navigation->setProperty('_validate', $_validate);
			if (false == $this->Navigation->create())
			{
				$this->error($this->Navigation->getError());
			}

			$data = array(
				'title'          => $_POST['title'],
				'url'            => $_POST['url'],
				'target'         => $_POST['target'],
				'position'       => $_POST['position'],
				'status'         => $_POST['status'],
				'display_order'  => intval($_POST['display_order']),
			);

			if (empty($navID))
			{
				$lastID = $this->Navigation->add($data);
                $message = L('nav.nav_added_succeed');
			}
			else
			{
				if (empty($navInfo))
				{
			 		$this->error(L('nav.nav_not_exist'));
				}
				$where = array('nav_id' => $navID);
				$this->Navigation->where($where)->save($data);
			}

			$message = L('nav.nav_edited_succeed');
			$this->Navigation->cacheNavigations();

			$this->assign('jumpUrl', __URL__.'/navigationList');
			$this->success($message);
		}
		else
		{
			$navStatusRadios = array(1 => L('yes'), 0 => L('no'));
			$navStatusChecked = isset($navInfo['status']) ? $navInfo['status'] : 1;
			$this->assign('navStatusRadios', $navStatusRadios);
			$this->assign('navStatusChecked', $navStatusChecked);

			$navTargetRadios = array(
				'_blank' => L('_blank'),
				'_self'  => L('_self'),
				'_top' => L('_top'),
			);
			$navTargetChecked = isset($navInfo['target']) ? $navInfo['target'] : '_blank';
			$this->assign('navTargetRadios', $navTargetRadios);
			$this->assign('navTargetChecked', $navTargetChecked);

			$navPositionRadios = array(
				'top' => L('nav.top'),
				'middle' => L('nav.middle'),
				'bottom' => L('nav.bottom'),
			);
			$navPositionChecked = isset($navInfo['position']) ? $navInfo['position'] : 'top';
			$this->assign('navPositionRadios', $navPositionRadios);
			$this->assign('navPositionChecked', $navPositionChecked);

			$this->assign('rs', $navInfo);
			$this->display('editNavigation');
		}
	}


	//管理导航
	public function manageNavigation()
	{
		if ($this->isPost())
		{
		    if (!empty($_POST['display_order']))
		    {
		     	foreach ($_POST['display_order'] as $key => $value)
		     	{
		     	    $data =array(
		     	        'status' => intval($_POST['status'][$key]),
		     	        'display_order' => intval($_POST['display_order'][$key]),
		     	    );
		     	    $map = array('nav_id' => $key);
		     	    $this->Navigation->where($map)->save($data);
		     	}
		    }

		    if (!empty($_POST['mods']))
		    {
		        $map = array('nav_id' => array('in', $_POST['mods']));
		    	$this->Navigation->where($map)->delete();
		    }

		    cache_navigations();
		    $this->success(L('nav.nav_updated_succeed'));
		}
	}

}


/* End of file NavigationAction.class.php */
/* Location: ./App/Lib/Action/Admin/NavigationAction.class.php */